﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PostDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnInvoke_Click(object sender, EventArgs e)
        {
            txtRet.Text = Post(txtUrl.Text, txtJson.Text);
        }

        /// <summary>
        /// 跨域访问
        /// </summary>
        /// <param name="url">通讯地址</param>
        /// <param name="param">json参数</param>
        /// <param name="time">超时</param>
        /// <returns></returns>
        public   string Post(string url, string param, int time = 60000)
        {
            string responseStr = string.Empty;

            WebRequest request = WebRequest.Create(url);
            request.Method = "Post";
            request.ContentType = "application/json";

            byte[] requestData = System.Text.Encoding.UTF8.GetBytes(param);
            request.ContentLength = requestData.Length;

            Stream newStream = request.GetRequestStream();
            newStream.Write(requestData, 0, requestData.Length);
            newStream.Close();

            var response = request.GetResponse();
            Stream ReceiveStream = response.GetResponseStream();
            using (StreamReader stream = new StreamReader(ReceiveStream, Encoding.UTF8))
            {
                responseStr = stream.ReadToEnd();
            }

            return responseStr;


            //byte[] bytes = Encoding.UTF8.GetBytes(param);
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            //request.Method = "POST";
            //request.ContentLength = bytes.Length;
            //request.ContentType = "application/json";
            //Stream reqstream = request.GetRequestStream();
            //reqstream.Write(bytes, 0, bytes.Length);

            ////声明一个HttpWebRequest请求  
            //request.Timeout = 90000;
            ////设置连接超时时间  
            //request.Headers.Set("Pragma", "no-cache");
            //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //Stream streamReceive = response.GetResponseStream();
            //Encoding encoding = Encoding.UTF8;

            //StreamReader streamReader = new StreamReader(streamReceive, encoding);
            //string strResult = streamReader.ReadToEnd();
            //streamReceive.Dispose();
            //streamReader.Dispose();

            //return strResult;


            //Uri address = new Uri(url);
            //HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
            //request.Method = "POST";
            //request.ContentType ="application/x-www-form-urlencoded;charset=utf-8";///"application/json;charset=utf-8"; // 
            //request.Timeout = time;
            //byte[] byteData = UTF8Encoding.UTF8.GetBytes(param == null ? "" : param);
            //request.ContentLength = byteData.Length;
            //using (Stream postStream = request.GetRequestStream())
            //{
            //    postStream.Write(byteData, 0, byteData.Length);
            //}
            //string result = "";
            //using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            //{
            //    StreamReader reader = new StreamReader(response.GetResponseStream());
            //    result = reader.ReadToEnd();
            //}
            //return (result);


        }
    }
}
